package vip.meeet;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.IOUtils;
import vip.meeet.utils.ESUtil;
import vip.meeet.utils.HttpUtil;

import java.io.FileInputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.List;

/**
 * 索引模板：https://www.elastic.co/guide/en/elasticsearch/reference/7.5/indices-templates.html
 * setting:
 */
public final class IndexHelper {

    /**
     * 删除索引
     * @param index
     * @throws IOException
     */
    public static void deleteIndex(String hostPort,String index) throws IOException {
        String urlBase = "http://%s/%s";
        String url = String.format(urlBase, hostPort,index);
        HttpUtil.delete(url);
    }

    public static void deleteIndex(String hostPort,List<String> indices) throws IOException {
        String ins = String.join(",", indices);
        deleteIndex(hostPort,ins);
    }

    /**
     * 删除索引数据
     * @param hostPort
     * @param index
     * @return
     * @throws IOException
     */
    public static String deleteIndexData(String hostPort,String index) throws IOException {
        String urlBase = "http://%s/%s/_doc/_delete_by_query?refresh";
//        String urlBase = "http://%s/%s/all/_delete_by_query";
        String url = String.format(urlBase,hostPort,index);
        String body = ESUtil.queryMatchAll();
        return HttpUtil.postJsonContent(url,body);
    }

    /**
     *
     * @param hostPort
     * @param index index索引名称必须小写
     * @return
     * @throws IOException
     */
    public static String createIndex(String hostPort,String index) throws IOException {
        String urlBase = "http://%s/%s";
        String url = String.format(urlBase, hostPort, index);
        JSONObject body = new JSONObject();
        body.put("settings",EsHelper.getDefaultSettings());
        return HttpUtil.putJsonContent(url,body.toJSONString());
    }

    public static String createIndex(String hostPort,String index,JSONObject settings,JSONObject mappings) throws IOException {
        String urlBase = "http://%s/%s";
        String url = String.format(urlBase, hostPort, index);
        JSONObject body = new JSONObject();
        body.put("settings",settings);
        body.put("mappings",mappings);
        return HttpUtil.putJsonContent(url,body.toJSONString());
    }

    public static String createIndex(String hostPort,String index,String settingsMappingsFile) throws IOException {
        String urlBase = "http://%s/%s";
        FileInputStream fileInputStream = new FileInputStream(settingsMappingsFile);
        List<String> list = IOUtils.readLines(fileInputStream, Charset.defaultCharset());
        String content = String.join("", list);
        String url = String.format(urlBase, hostPort, index);
        return HttpUtil.putJsonContent(url,content);
    }

    /**
     * 获取索引setting
     * https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-update-settings.html
     * @param hostPort
     * @param index
     * @return
     * @throws IOException
     */
    public static String getSetting(String hostPort,String index) throws IOException {
//        String urlBase = "http://%s/%s/_settings?pretty";
        String urlBase = "http://%s/%s/_settings";
        String url = String.format(urlBase, hostPort, index);
        String settings = HttpUtil.getContent(url);
        return settings;
    }

    public static String closeIndex(String hostPort,String index) throws IOException {
        String urlBase = "http://%s/%s/_close";
        String url = String.format(urlBase,hostPort,index);
        String content = HttpUtil.postContent(url);
        return content;
    }

    public static String openIndex(String hostPort,String index) throws IOException {
        String urlBase = "http://%s/%s/_open";
        String url = String.format(urlBase,hostPort,index);
        String content = HttpUtil.postContent(url);
        return content;
    }


}
